<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class OrderController extends BaseController {

    //订单列表
	public function index() {
        $data = array_filter($_GET);
        if (!empty($_GET['user_nickname'])) {
            $data['user_nickname'] = array('like',"%".trim($_GET['user_nickname'])."%");
        }
        unset($data['p']);
        $count = M('order')->alias('o')->field('o.*,u.user_id,u.user_name,u.user_phone,u.user_nickname')->join('left join midi_user as u on o.order_user_id = u.user_id')->where($data)->count();
        $page = getpage($count,10);
        $order_arr = M('order')->alias('o')->field('o.*,u.user_id,u.user_name,u.user_phone,u.user_nickname')->join('left join midi_user as u on o.order_user_id = u.user_id')->where($data)->order('order_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('order_arr', $order_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //订单详情
    public function order_details(){
        $data['order_id'] = I('get.order_id');
        $order_arr = M('order')->alias('o')->join('left join midi_user as u on o.order_user_id = u.user_id')->join('left join midi_address as a on o.order_address = a.address_id')->where($data)->find();
        $details = $order_arr['order_details'];
        $details_arr = explode(',', $details);
        $goods_arr = array();
        foreach ($details_arr as $v) {
            $garr = explode(':', $v);
            $goods['goods_id'] = $garr[0];
            $goods = M('goods')->where($goods)->find();
            $goods['goods_quantity'] = $garr[1];
            array_push($goods_arr, $goods);
            unset($goods);
        }
        $this->assign('goods_arr',$goods_arr);
        $this->assign('order_arr',$order_arr);
        $this->display();
    }

    //修改订单状态
    public function order_edit(){
        if (I('post.action') == 'order_edit_action') {
            $this->order_edit_action();
            exit();
        }
        $order_arr = M('order')->where($_POST)->find();
        echo json_encode($order_arr);
    }

    //修改订单状态操作
    public function order_edit_action(){
        if(M('order')->save($_POST)){
            $this->redirect('index');
        }else{
            $this->error('修改订单失败！');
        }
    }

    //快递公司查询
    public function express_list(){
        $express_com = I('get.com');
        $order_express_json = expressList($express_com,100);
        $express_arr = json_decode($order_express_json,true);
        $express_list = $express_arr['showapi_res_body']['expressList'];
        $this->assign('express_list',$express_list);
        $this->display();

    }

    //修改运费
    public function freight_edit(){
        if (I('post.action') == 'freight_edit_action') {
            $this->freight_edit_action();
            exit();
        }
        $freight_arr = M('freight')->find();
        $this->assign('freight_arr',$freight_arr);
        $this->display();
    }

    //修改运费处理
    public function freight_edit_action(){
        if (M('freight')->save($_POST)) {
            $this->success('修改运费成功！');
        }else{
            $this->error('修改运费失败！');
        }
    }


}